<?php // 4.0
class invoice {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('companies');
    $masterclass->loadmodule('payments');
    $masterclass->loadmodule('invoices_wap',$masterclass->mainconfig['moduledirectory'].'invoices/invoices_wap.func.php');
    
    $masterclass->websiteconfig['title']='Show Invoice';
    
    if ($masterclass->allclasses['invoices']->checkinvoicesid($masterclass->sitevars['invoicesid'])===false) {
      echo 'INVOICE DOES NOT EXIST!';
      exit();
    }

    $masterclass->site['postfields'][] = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'fields'=>array(
        'documentid'=>'invoices_documentid',
        'documentdate'=>'invoices_documentdate',
        'duedate'=>'invoices_documentduedate',

        'net'=>'invoices_net',
        'paid'=>'invoices_paid',

        'recipientlineone'=>'invoices_recipientlineone',
        'recipientlinetwo'=>'invoices_recipientlinetwo',
        'recipientlinethree'=>'invoices_recipientlinethree',
        'recipientlinefour'=>'invoices_recipientlinefour',
        'recipientlinefive'=>'invoices_recipientlinefive',
        'companylineone'=>'invoices_companylineone',
        'companylinetwo'=>'invoices_companylinetwo',
        'companylinethree'=>'invoices_companylinethree',
        'companylinefour'=>'invoices_companylinefour',
        'companylinefive'=>'invoices_companylinefive',
        
        'customer'=>'customerid'
      )
    );
    $masterclass->site['postfields'][] = array(
      'trackingid'=>'customerid'
    );
    
    $masterclass->sitevars['invoices_payments']=$masterclass->allclasses['invoices']->fetch_invoices_payments($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_invoicetotals']=$masterclass->allclasses['invoices']->fetch_invoices_totals($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_totalpaymentamt']=0;
    if (!empty($masterclass->sitevars['invoices_payments'])) {
      foreach ($masterclass->sitevars['invoices_payments'] as $payment) {
        $masterclass->sitevars['invoices_totalpaymentamt']+=$payment['invoicepayment'];
      }
    }
    
  return;
  }
  
  function savedata() {
    global $masterclass;


  return;
  }

  function display_invoices_itemslist($listarray=array()) {
    global $masterclass;
    $arrret=array();

    $ret='';
    if (empty($listarray)) {
      $ret.='No Items';
    } else {      
      foreach ($listarray as $result) {
        $pname='';
        $profile='';
        if ($listedids!='') { $listedids.=','; } $listedids.=$result['id'];

        if (date("Y-m-d")==date("Y-m-d",strtotime($result['updated']))) {
          $pstamp = date("H:m:s",strtotime($result['updated']));
        } else {
          $pstamp = date("Y-m-d",strtotime($result['updated']));
        }

        if (preg_match('/^[0-9.-]+$/iD', $result['qty'])) { $qty=$result['qty']; } else { $qty=1; }
        if (preg_match('/^[0-9.-]+$/iD', $result['cost'])) { $cost=$result['cost']; } else { $cost=0; }
        if (preg_match('/^[0-9.-]+$/iD', $result['taxpercent'])) { $taxpercent=$result['taxpercent']; } else { $taxpercent=0; }

        if ($cost<0) { $polarity='-'; } else { $polarity='+'; }

        $subtotal=$qty*abs($cost);
        $taxcost=$subtotal*($taxpercent/100);
	    $total=$taxcost+$subtotal;

        $profile.=$result['qty'];
        $profile.='&nbsp;';
        if (preg_match('/^[0-9.-]+$/iD', $total)) { $profile.='$'.number_format($total,2); } else { $profile.=$total; }
        $profile.='&nbsp;';
        $profile.=$result['name'];

        if ($result['description']!='') {
          $profile.='<br>';
	      $profile.=nl2br($result['description']);
        }
        $profile.='<hr>';
        $ret.=$profile;
      }
    }

    $arrret['load_list']=$ret;
    $arrret['load_containername']=$containername;
    
    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }
  
  function display() {
    global $masterclass;

    $arrdocumentdates=$masterclass->allclasses['invoices']->fetch_invoicesdocumentdates($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_documentdate']=$arrdocumentdates[0];
    $masterclass->sitevars['invoices_duedate']=$arrdocumentdates[1];
    $masterclass->sitevars['invoices_paid']=$arrdocumentdates[2];
    
    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00' || $masterclass->sitevars['invoices_documentdate']=='') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00' || $masterclass->sitevars['invoices_duedate']=='') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }
    if ($masterclass->sitevars['invoices_paid']=='0000-00-00' || $masterclass->sitevars['invoices_paid']=='') { $masterclass->sitevars['invoices_paid']=''; } else { $masterclass->sitevars['invoices_paid']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_paid'])); }

    $arrinvoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($masterclass->sitevars['invoicesid']);
    $arrinvoicetotal=$masterclass->allclasses['invoices']->calc_invoicetotal($arrinvoiceitems);
    
    echo '<a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'">Home</a> | <a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/">Invoices</a> | <a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'customers/">Customers</a><hr>';
    echo '<center><b>'.$masterclass->sitevars['invoices_recipientlineone'].' #'.$masterclass->sitevars['invoices_documentid'].' $'.number_format($arrinvoicetotal['total'],2).'</b></center><hr>';

    echo 'Doc #:&nbsp;'.$masterclass->allclasses['websites']->disp_text('invoices_documentid').'<br>';
    echo 'Date:&nbsp;'.$masterclass->allclasses['websites']->disp_text('invoices_documentdate').'<br>';
    echo 'Terms:&nbsp;';
    if ($masterclass->sitevars['invoices_net']=='0') { echo 'Upon Receipt<br>'; } else { echo 'Net '.$masterclass->allclasses['websites']->disp_text('invoices_net').'<br>'; }
    echo 'Due&nbsp;Date:&nbsp;'.$masterclass->allclasses['websites']->disp_text('invoices_duedate').'<br>';
    echo 'Paid:&nbsp;'.$masterclass->allclasses['websites']->disp_text('invoices_paid').'<br>';

    echo '<br><center><b>Recipient</b></center><hr>';
    if ($masterclass->sitevars['invoices_recipientlineone']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlineone').'<br>'; }
    if ($masterclass->sitevars['invoices_recipientlinetwo']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinetwo').'<br>'; }
    if ($masterclass->sitevars['invoices_recipientlinethree']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinethree').'<br>'; }
    if ($masterclass->sitevars['invoices_recipientlinefour']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinefour').'<br>'; }
    if ($masterclass->sitevars['invoices_recipientlinefive']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_recipientlinefive').'<br>'; }
  
    echo '<br><center><b>Company</b></center><hr>';
    if ($masterclass->sitevars['invoices_companylineone']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_companylineone').'<br>'; }
    if ($masterclass->sitevars['invoices_companylinetwo']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_companylinetwo').'<br>'; }
    if ($masterclass->sitevars['invoices_companylinethree']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_companylinethree').'<br>'; }
    if ($masterclass->sitevars['invoices_companylinefour']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_companylinefour').'<br>'; }
    if ($masterclass->sitevars['invoices_companylinefive']!='') { echo $masterclass->allclasses['websites']->disp_text('invoices_companylinefive').'<br>'; }

    echo '<br><center><b>Payments</b></center><hr>';
    
    echo '<br><center><b>Invoice Totals</b></center><hr>';
    echo number_format($arrinvoicetotal['qty'],0).' - Item Count<br>';
    echo '$'.number_format($arrinvoicetotal['subtotal'],2).' - SubTotal<br>';
    echo '$'.number_format($arrinvoicetotal['tax'],2).' - Tax<br>';
    echo '$'.number_format($arrinvoicetotal['total'],2).' - Total<br>';
    
    echo '<br><center><b>Invoice Items</b></center><hr>';
    $listdata=$masterclass->allclasses['invoices_wap']->display_invoices_itemslist($arrinvoiceitems);
    echo $listdata['load_list'];
    
  return;
  }
}
?>